package com.ruoyi.web.controller.app;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.HgtOperationDo;
import com.ruoyi.system.domain.HgtSumDo;
import com.ruoyi.system.domain.HgtpieDo;
import com.ruoyi.system.domain.MyCost;
import com.ruoyi.system.service.HgtOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;


@RestController
@RequestMapping("/ktce")
public class ktcsController {

    @Autowired
    private HgtOperationService hgtOperationService;

    @PostMapping ("ktsave")
    HashMap<Object, Object> ktsave( @RequestBody HgtOperationDo hgtOperationDo) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = new Date();
        String currentTime = dateFormat.format(date1);
        hgtOperationDo.setCreatetime(currentTime);
        hgtOperationService.saveHgtOperation(hgtOperationDo);
        aa.put("code",0);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping ("ktupdate")
    HashMap<Object, Object> ktupdate( @RequestBody HgtOperationDo hgtOperationDo) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = new Date();
        String currentTime = dateFormat.format(date1);
        hgtOperationDo.setOpetime(currentTime);
        hgtOperationService.updateHgtOperation(hgtOperationDo);
        aa.put("code",0);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    //饼图统计
    @PostMapping("tjpie")
    HashMap<Object, Object> tjpie() {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        List<HgtpieDo> hgtSumDo = hgtOperationService.findSumHgtOperation();
        aa.put("series",hgtSumDo);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("tjpie2")
    HashMap<Object, Object> tjpie2() {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        List<HgtpieDo> hgtSumDo = hgtOperationService.findSumHgtOperation2();
        aa.put("series",hgtSumDo);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("findphonerole")
    HashMap<Object, Object> findphonerole(@RequestBody Map<String, Object> params) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        HashMap<Object, Object> bb = new HashMap<>();
        String phonerole = hgtOperationService.getuserfindphonerole(params.get("creatorname").toString());
        bb.put("menustate",false);
        bb.put("menustate2",false);
        bb.put("menustate3",false);
        bb.put("menustate4",false);
        bb.put("menustate5",false);
        bb.put("menustate6",false);
        bb.put("menustate7",false);
        bb.put("menustate8",false);
        bb.put("menustate9",false);
        bb.put("menustate10",false);
        bb.put("menustatest1",false);
        bb.put("menustatest2",false);
        bb.put("menustatest3",false);
        bb.put("menustatest4",false);
        bb.put("menustateyy1",false);
        bb.put("menustateyy2",false);
        bb.put("menustateyy3",false);
        if(phonerole.equals("admin")){
            bb.put("menustate",true);
            bb.put("menustate2",true);
            bb.put("menustate3",true);
            bb.put("menustate4",true);
        } if(phonerole.contains("lc")){
            bb.put("menustate5",true);
            bb.put("menustate6",true);
            bb.put("menustate7",true);
            bb.put("menustate9",true);

        } if(phonerole.contains("saaslead")){
            bb.put("menustate7",true);
            bb.put("menustate10",true);

        } if(phonerole.contains("saas")){
            bb.put("menustate7",true);
            bb.put("menustate10",true);
        } if(phonerole.equals("lcb")){
            bb.put("menustate5",true);
            bb.put("menustate6",true);
        } if(phonerole.equals("lcB")){
            bb.put("menustate6",true);
        } if(phonerole.equals("lcA")){
            bb.put("menustate6",true);
            bb.put("menustate8",true);
            bb.put("menustate9",true);

        }if(phonerole.contains("zhst")){
            bb.put("menustatest1",true);
            bb.put("menustatest2",true);
            bb.put("menustatest3",true);
            bb.put("menustatest4",true);
        }if(phonerole.contains("yysq")){
            bb.put("menustateyy1",true);
        }if(phonerole.contains("yyld")){
            bb.put("menustateyy2",true);
        }if(phonerole.contains("yygl")){
            bb.put("menustateyy3",true);
        }
        aa.put("code",0);
        aa.put("msg","提交成功");
        aa.put("phoneMenu",bb);
        map.put("data",aa);
        return map;
    }
    @PostMapping("loginktxt")
    HashMap<Object, Object> loginktxt(@RequestBody Map<String, Object> params) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        HgtOperationDo bb = new HgtOperationDo();
        int cc = hgtOperationService.findUser(params.get("username").toString(), params.get("password").toString());
        if (cc==1){
            aa.put("code",0);
            aa.put("msg","提交成功");
            map.put("data",aa);
            return map;
        }else{
            aa.put("code",-1);
            aa.put("msg","提交成功");
            map.put("data",aa);
            return map;
        }

    }
    @PostMapping("findHgtOperationBycreator")
    HashMap<Object, Object> findHgtOperation(@RequestBody Map<String, Object> params) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        List<HgtOperationDo> hgtSumDo = hgtOperationService.findHgtOperationsearch(params.get("creatorname").toString(),(String)params.get("search"));
        aa.put("code",0);
        aa.put("msg","提交成功");
        aa.put("resultData",hgtSumDo);
        map.put("data",aa);
        return map;
    }
    @GetMapping("findHgtOperationByid")
    HashMap<Object, Object> findHgtOperationByid(String id) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();

        List<HgtOperationDo> hgtSumDo = hgtOperationService.findHgtOperation(id);
        aa.put("code",0);
        aa.put("msg","提交成功");
        aa.put("resultData",hgtSumDo);
        map.put("data",aa);
        return map;
    }
    @GetMapping("findphoneMenu")
    HashMap<Object, Object> findphoneMenu(String username) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @GetMapping("/export")
    public void exportExecl(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("aaaaaaaaaaaaaaaaa");
        List<HgtOperationDo> hgtSumDo = hgtOperationService.findHgtOperationall();
        OutputStream outputStream=response.getOutputStream();
        try {
            this.setExcelResponseProp(response, "问题反馈表");
            // 模拟根据条件在数据库查询数据

            //这个实现方式非常简单直接，使用EasyExcel的write方法将查询到的数据进行处理，以流的形式写出即可
            EasyExcel.write(outputStream,HgtOperationDo.class)//对应的导出实体类
                    .excelType(ExcelTypeEnum.XLSX)//excel文件类型，包括CSV、XLS、XLSX
                    .sheet("问题反馈表")//导出sheet页名称
                    .doWrite(hgtSumDo); //查询获取的数据集合List<T>，转成excel
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            outputStream.flush();
            outputStream.close();
        }
    }
    /**
     * 头像上传
     */
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty()){
            String avatar =FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = success();
            ajax.put("imgUrl", avatar);
            return ajax;
        }else{
            return error("上传图片异常，请联系管理员");
        }

    }
    /**
     * 头像上传
     */
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        ExcelUtil<HgtOperationDo> util = new ExcelUtil<HgtOperationDo>(HgtOperationDo.class);
        List<HgtOperationDo> myHgtOperationDoList = util.importExcel(file.getInputStream());

        String message = hgtOperationService.importCost(myHgtOperationDoList);


        return success(message);
    }
    private void setExcelResponseProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException {
        //设置内容类型
        response.setContentType("application/vnd.vnd.ms-excel");
        //设置编码格式
        response.setCharacterEncoding("utf-8");
        //设置导出文件名称（避免乱码）
        String fileName = URLEncoder.encode(rawFileName.concat(".xlsx"), "UTF-8");
        // 设置响应头
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName);
    }
}
